//
//  ForgetPassWordViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "ForgetPassWordViewController.h"
#import "ForgetPassWordModel.h"
#import <MBProgressHUD/MBProgressHUD.h>

@interface ForgetPassWordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *iphoneField;

@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@property (weak, nonatomic) IBOutlet UITextField *resetPwdField;

@property (weak, nonatomic) IBOutlet UITextField *yanzhenField;

@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [_iphoneField becomeFirstResponder];
}
- (IBAction)mailYanZhenSend:(id)sender {
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [hud show:YES];
    NSLog(@"%@",_iphoneField.text);
    [ForgetPassWordModel getYanzhenByPhone:_iphoneField.text success:^(NSString *data) {
        [hud hide:YES];
        [hud removeFromSuperview];
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"已发送" message:@"请等待验证短信" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [av show];
    } failure:^(NSString *message) {
        NSString* er = [NSString stringWithFormat:@"错误信息:%@",message];
        [hud hide:YES];
        [hud removeFromSuperview];
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"获取错误" message:er delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [av show];
    }];
}

- (IBAction)btn_submit:(id)sender {
    
    if (![_pwdField.text isEqualToString:_resetPwdField.text]) {
        
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"输入错误" message:@"两次输入的密码不同" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [av show];
        self.pwdField.text = @"";
        self.resetPwdField.text = @"";
        [self.pwdField becomeFirstResponder];
        return;
    }
    
    [self sendHttpToChangePwd];
}

- (void)sendHttpToChangePwd
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [hud show:YES];
    NSLog(@"%@ %@ %@",_iphoneField.text,_pwdField.text,_yanzhenField.text);
    [ForgetPassWordModel forgetByPhone:_iphoneField.text password:_pwdField.text yanzhen:_yanzhenField.text success:^{
        [hud hide:YES];
        [hud removeFromSuperview];
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"验证成功" message:@"修改密码成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [av show];
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSString *message) {
        [hud hide:YES];
        [hud removeFromSuperview];
        NSString *mes = [NSString stringWithFormat:@"验证错误:%@",message];
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"验证错误" message:mes delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [av show];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)p_back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
